<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
         class Queue {
            constructor() {
                this.items = [];
            }
            // 从后面添加元素
            enqueue(ele) {
                this.items.push(ele);
            }
            // 从前面取出第一个元素
            dequeue() {
                return this.items.shift();
            }
            // 获取第一个元素的值
            front() {
                return this.items[0]
            }
            // 判断是否为空
            isEmpty() {
                return this.items.length == 0;
            }
            // 判断元素个数
            size() {
                return this.items.length;
            }
            // 清空
            clear() {
                return this.items = [];
            }
        };

        function hotPotato(arr,n) {
            let queue = new Queue();
            for (let i = 0; i < arr.length; i++) {
                queue.enqueue(arr[i]);
            }

            while(queue.size()>1){
                let loseName = '';
                for (let i = 0; i < n; i++) {
                    queue.enqueue(queue.dequeue());
                }
                loseName = queue.dequeue();
                console.log('被淘汰的是'+loseName);
            }

            return queue.dequeue();
        }

        console.log('胜利者是'+ hotPotato([1,2,3,4,5,6],1));
    </script>
</body>
</html>